package com.longge.wechat.controller.wx;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.longge.wechat.communication.Poster;
import com.longge.wechat.message.resp.RespTextMessage;
import com.longge.wechat.utils.XmlUtil;

@Controller
public class TextController {

	
	@RequestMapping(value = "/text.do", method = RequestMethod.POST)
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String openId = (String) request.getAttribute("openId");
		String from = (String) request.getAttribute("from");
//		String xmlText = (String) request.getAttribute("xmlText");
//		ReqTextMessage rtm = (ReqTextMessage) XmlUtil.getInstance().xml2bean(
//				xmlText, ReqTextMessage.class);
//		String reciecedMark = "";
//		String reciecedUseful = "";
//		if(rtm.getContent().length() >= 4){
//			reciecedMark = rtm.getContent().substring(0, 4).toLowerCase();
//			reciecedUseful = rtm.getContent().substring(4);
//		}
		String rs = "消息已收到";
//		if ("nops".equals(reciecedMark)) {
//			ParkingEntity entity = new ParkingEntity();
//			if (parkingServiceImpl.findParkingById(1) == null) {
//				entity.setNumber(0);
//				entity.setAddTime(new Date());
//				parkingServiceImpl.saveParking(entity);
//			}
//			if (SignUtil.isNotEmpty(reciecedUseful)) {
//				int number = Integer.valueOf(reciecedUseful);
//				entity = parkingServiceImpl.findParkingById(1);
//				entity.setNumber(number);
//				entity.setAddTime(new Date());
//				parkingServiceImpl.updateParking(entity);
//				rs = "剩余车位数已更新成功，谢谢配合！";
//			}
//		}

		RespTextMessage message = new RespTextMessage(openId, from, rs);
		Poster.post(response,
				XmlUtil.getInstance().bean2xml(message, null, null));
	}
}
